// 给手机号加密
/**
  加密方式为:
  第二位数字+1然后和第三位进行交换
  倒数后四位倒序
**/
export default {
  encryption (p) {
    let phone = p
    if (this.isPhone(phone)) {
      let key = phone
      let result = this.keysplit(phone)
      if (result.length >= 3) {
        key = this.h(result[0]) + result[1] + this.f(result[2])
      }
      return key
    } else {
      return phone
    }
  },
  isPhone (phone) {
    let reg = /^1[345678]\d{9}$/
    return reg.test(phone)
  },
  h (phone) {
    let keya = parseInt(phone.substr(0, 1))
    let keyb = parseInt(phone.substr(1, 1)) + 1
    let keyc = parseInt(phone.substr(2, 1))
    return String(keya) + keyc + keyb
  },
  f (phone) {
    let keya = parseInt(phone.substr(0, 1))
    let keyb = parseInt(phone.substr(1, 1))
    let keyc = parseInt(phone.substr(2, 1))
    let keyd = parseInt(phone.substr(3, 1))
    return String(keyd) + keyc + keyb + keya
  },
  keysplit (phone) {
    let a = []
    a.push(String(phone).substr(0, 3))
    a.push(String(phone).substr(3, 4))
    a.push(String(phone).substr(7, 4))
    // console.log(a)
    return a
  }
}
